<?php

namespace app\admin\controller;

use app\model\Area;
use think\App;
use think\exception\ValidateException;
use app\model\Technical;
use app\model\Category;
use app\model\Store;

class TechnicalController extends Base
{

	function index()
	{
		$uniacid = uniacid();
		$keyword = input('post.keyword', '', 'serach_in');
		$cate_ids = input('post.cate_ids', '', 'serach_in');
		$status = input('post.status', '', 'serach_in');
		$query = Technical::where(['uniacid' => $uniacid]);

		if (!empty($this->sid)) {
			$query->where('sid', $this->sid);
		}
		if (!empty($this->ocid)) {
			$query->where('ocid', $this->ocid);
		}

		if (!empty($keyword)) {
			$query->where('name', 'like', '%' . $keyword . '%');
		}

		if (!empty($cate_ids)) {
			$query->where('cat_id',  $cate_ids);
		}

		if (!empty($status) || $status === "0") {
			$query->where(['status' => $status]);
		}

		$res = $query->order('id desc')
			->paginate(getpage())
			->toArray();

		foreach ($res['data'] as &$vo) {
			$vo['image'] = toimg($vo['image']);
			if ($vo['sid'] == 0) {
				$vo['name'] = '[自营]' . $vo['name'];
			} else {
				$vo['name'] = '[' . Store::getTitle($vo['sid']) . ']' . $vo['name'];
			}
			$vo['create_time'] = time_format($vo['create_time']);
			$vo['update_time'] = time_format($vo['update_time']);
		}
		$data['data'] = $res;
		return $this->json($data);
	}

	public function getPosition(){
        $id = input('get.id');
        if(empty($id)){
            return $this->json(['errno'=>-1,' 师傅不存在']);
        }

        $tech = Technical::field('id,latitude,longitude,province_name,city_name,district_name,region_name')->find($id);
        if(empty($tech)){
            $tech = [];
        }else{
            $tech = $tech->toArray();
        }

        if (!empty($tech['province_name'])) {
            $tech['province_id'] = Area::get_area_id($tech['province_name']);
        }
        if (!empty($tech['city_name'])) {
            $tech['city_id'] = Area::get_area_id($tech['city_name']);
        }
        if (!empty($tech['district_name'])) {
            $tech['district_id'] = Area::get_area_id($tech['district_name']);
        }

        $area[0] = $tech['province_id'];
        $area[1] = $tech['city_id'];
        if (!empty($tech['district_id'])) {
            $area[2] = $tech['district_id'];
        }

        $tech['area'] = $area;

        return $this->json(['data'=>$tech]);
    }
}
